package com.mjkj.order_feign.controller;

import com.mjkj.apis.PayFeignApi;
import com.mjkj.core.result.Result;
import com.mjkj.core.result.ResultGenerator;
import io.github.resilience4j.bulkhead.annotation.Bulkhead;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Tag(name = "feign管理", description = "feign管理")
@Slf4j
public class OrderController {

    @Resource
    private PayFeignApi payFeignApi;

    @GetMapping("/feign-order/list")
    @Operation(summary = "feign支付列表", description = "feign支付列表")
    public Result list(){
        Result result = payFeignApi.list();
        return result;
    }

    @GetMapping("/feign-order/get/{id}")
    @CircuitBreaker(name = "cloud-payment-service", fallbackMethod = "myFallback")
    @Operation(summary = "circuit支付详情id", description = "circuit支付详情id")
    public Result get(@PathVariable(value = "id") Long id){
        Result result = payFeignApi.get(id);
        return result;
    }

    @GetMapping("/feign-order/bulk/{id}")
    @Bulkhead(name = "cloud-payment-service",fallbackMethod = "myFallback",type = Bulkhead.Type.SEMAPHORE)
    @Operation(summary = "bulk支付详情id", description = "bulk支付详情id")
    public Result get2(@PathVariable(value = "id") Long id){
        Result result = payFeignApi.get(id);
        return result;
    }

    @GetMapping("/feign-order/limiter/{id}")
    @RateLimiter(name = "cloud-payment-service",fallbackMethod = "myFallback")
    @Operation(summary = "limiter支付详情id", description = "limiter支付详情id")
    public Result get3(@PathVariable(value = "id") Long id){
        Result result = payFeignApi.get(id);
        return result;
    }

    /**
     * 服务降级处理方法, 与全局异常冲突
     * @param id
     * @param e
     * @return
     */
    public Result myFallback(Long id, Throwable e){
        log.error("feign-order服务降级处理", e);
        return ResultGenerator.genFailResult("feign-order服务降级处理");
    }
}
